<?php
  header("Content-type:text/html;charset=utf-8");
  $dbhost = $_POST['dbhost'];
  $dbuser = $_POST['dbuser'];
  $dbpwd  = $_POST['dbpwd'];
  $dbname  = "oa";
  
  if(empty($dbhost)||(empty($dbuser))){
	echo "<script>alert('对不起,数据库地址和数据库用户名不能为空');history.go(-1);</script>";
	exit();
  }
  $str = "";
  $fp = fopen("sql.txt","rb");
  
  while(!feof($fp)){
	 $order = fgetc($fp);
	 if(!feof($fp)){
	     if($order==";"){
			$str .= ";"; 
		 }else{
			$str .=$order;
		 }
	 }
  }
  fclose($fp);
  
  $link = mysql_pconnect("{$dbhost}", "{$dbuser}","{$dbpwd}")or die("Could not connect: " . mysql_error());
  
  
  $result = mysql_query("CREATE DATABASE {$dbname} DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
");
  if($result){
	 mysql_select_db("{$dbname}");
  }
  mysql_query("set names gbk");
  $create_data = explode(";",$str);
  for($i=0;$i<count($create_data);$i++){
	  $sql = $create_data[$i];
	  $result = mysql_query($sql); 
  }

  $rs =  mysql_query("show tables from {$dbname}");
  $j=0;
  while($row = mysql_fetch_row($rs)){
        $j++;
  }

  if($j==43){
	 touch("install_lock.txt");
     echo "<script>location.href='step-4.php';</script>";
  }else{
	 mysql_query("drop database {$dbname}");
	 if(file_exists("install_lock.txt")){
		unlink("install_lock.txt"); 
	 }
     echo "<script>alert('对不起,安装失败');</script>";
  }




  





?>